class CouponsController < ApplicationController
  def show
    @coupons = CouponInfo.find(:all, :conditions => {:customer_id => params[:id]})
    
    data = Hash.new("data")
    status = Hash.new("status")
    h = Hash.new
    
    if @coupons.count < 1
      status["result"] = "e"
      status["message"] = "empty"
    else
      status["result"] = "s"
      @coupons.each do |coupon|
        c = Hash.new("coupon")
        c["shop_name"] = coupon.shop.name
        c["count"] = coupon.count
        data[coupon.shop.id] = c
      end
      h["data"] = data
    end
    
    h["status"] = status
    
    render :json => h
    #render :json => @coupons
  end

  def new
  end

  def create
    unless params[:c].nil?
      @customer = User.find(params[:c])
    end
    unless params[:s].nil?
      @shop = Shop.find(params[:s])
    end
    unless params[:count].nil?
      @count = Integer(params[:count])
    end
    unless @customer.nil? && @shop.nil? && @count.nul?
      @coupon_history = CouponHistory.new
      @coupon_history.customer = @customer
      @coupon_history.shop = @shop
      @coupon_history.count = @count
      @coupon_history.save
      
      @coupon_info = CouponInfo.find(:first, :conditions => {:customer_id => params[:c], :shop_id => params[:s]})
      if @coupon_info.nil?
        @coupon_info = CouponInfo.new
        @coupon_info.customer = @customer
        @coupon_info.shop = @shop
        @coupon_info.count = 0
      end
      @coupon_info.count += @count
      @coupon_info.save
      
      render :json => @coupon_info
    end
  end
  
end
